<?php

namespace app\common\model\lease;

use think\Model;
use traits\model\SoftDelete;

class Lease extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'lease';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'state_text',
        'image_str',
        'images_array',
    ];
    public function getImageStrAttr($value, $data)
    {
        if (isset($data['image']) && $data['image']) {
            return cdnurl($data['image'], true);
        }
        return '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        if (isset($data['images']) && $data['images']) {
            $images = explode(',', $data['images']);
            foreach ($images as &$v) {
                $v = cdnurl($v, true);
            }
            return $images;
        }
        return [];
    }
    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function category()
    {
        return $this->belongsTo('Category', 'c_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function fCate()
    {
        return $this->belongsTo('Category', 'f_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
